<template>
  <!--关键岗位信息	-->
  <div>
    <FilterTable
      height="auto"
      ref="tableQuestion"
      :columns="columns"
      :isSelectColumn="isSelectColumn"
      selectionType="single"
      :isMemoryPagination="true"
      :onSyncTableData="syncFun"
      :datakey="'records'"
      :isToperInfo="false"
      :onColumnsChange="columns => (this.columns = columns)"
      columnsRef="notification_maintain_info_02">

      <template slot="controls">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addFun()">新增</el-button>
      </template>
    </FilterTable>
  </div>
</template>

<script>
  import { dictionary } from '@/utils/dictMixin';
  import * as types from '@/api/cms-api/origin/originInfo/types';

  import vm from './bridge';

  export default {
    mixins: [dictionary],
    model: {
      prop: 'pdata',
      event: 'click'
    },
    props: {
      pdata: {
        type: Object,
        default: () => ({})
      },
      formType: {
        type: String,
        required: true
      },
      formOptions: {
        type: Object,
        required: true
      }
    },
    data() {
      return {
        initForm: this.initFormFun(),
        columns: this.createColumns(),
        isSelectColumn: false,
        saveData: {
          editType: '',
          xxList: []
        },
      };
    },
    mounted() {
      //注册经销商类型变化时触发的事件
      //在审核时，给此页面的类型为只读，但是基本信息（infoBase可以编辑，会显示会触发事件，事件会触发禁用取消，覆盖整个表单的类型（表单类型为show，但是依旧可以输入，（部分）））
      if (this.formType != 'show') {
        vm.$on('changeDealerType', this.changeDealerType);
      }
    },
    methods: {
      //初始化数据
      init(data) {
        this.initForm.forEach(x => {
          if (this.pdata[x.fieldName]) {
            x.initialValue = this.pdata[x.fieldName];
          }
        });
      },
      //初始化数据（带触发事件，为经销商提报添加的）
      initPre(data) {
        if (this.formType != 'show') {
          if (!this.pdata.companyName) {
            this.pdata.companyName = this.pdata.dealerName;
          }
          // vm.$emit('changeDealerType', this.pdata);
          vm.$emit('changeDealerType', this.pdata.dealerType);
          // this.supplementChangeDealerType(this.pdata.dealerType);
        }
        this.initForm.forEach(x => {
          if (this.pdata[x.fieldName]) {
            x.initialValue = this.pdata[x.fieldName];
          }
        });
      },
      //表单验证通过后
      formChangeHandle(val) {
        this.pdata = Object.assign(this.pdata, val);
        //传递数据到父组件里
        this.$emit('click', this.pdata);
        //触发外部的事件，以标志着本表单已经准备完毕
        this.$emit('formchange', this.pdata);
      },
      //开放的提交方法
      saveDialog() {
        this.$refs.FormPanel.SUBMIT_FORM();
      },
      //经销商类型变更时
      changeDealerType(value) {
        //判断是否是dle页面，是则使用dlr专用验证
        let suffix = this.formOptions.formType === types.DEALER_INFO_FORM_STATUS_SUBMISSION_DLR ? 'DLR' : '';
        //根据类型处理验证
        if (types.DEALER_INFO_DEALER_TYPE_4S === value) {
          //4S
          vm.changeForm(this.initForm, '4S' + suffix, false);
          vm.clearValidate(this.$refs.FormPanel);
        } else if (types.DEALER_INFO_DEALER_TYPE_SINGLE_AFTER_SALE === value) {
          //单一售后
          vm.changeForm(this.initForm, 'SH' + suffix, false);
          vm.clearValidate(this.$refs.FormPanel);
        } else if (types.DEALER_INFO_DEALER_TYPE_SINGLE_SALE === value) {
          //单一销售
          vm.changeForm(this.initForm, 'XS' + suffix, false);
          vm.clearValidate(this.$refs.FormPanel);
        } else if (types.DEALER_INFO_DEALER_TYPE_DIRECT_SHOP === value) {
          //SPACE
          vm.changeForm(this.initForm, 'SPACE' + suffix, false);
          vm.clearValidate(this.$refs.FormPanel);
        } else if (types.DEALER_INFO_DEALER_TYPE_NO_DIRECT_SHOP === value) {
          //RACE
          vm.changeForm(this.initForm, 'RACE' + suffix, false);
          vm.clearValidate(this.$refs.FormPanel);
        } else if (types.DEALER_INFO_DEALER_TYPE_BIG_CUSTOMER === value) {
          //大客户
          vm.changeForm(this.initForm, 'DKH' + suffix, false);
          vm.clearValidate(this.$refs.FormPanel);
        }
        //this.supplementChangeDealerType(value);
      },
      //补充验证
      supplementChangeDealerType(value) {
        if (this.formOptions.formType === types.DEALER_INFO_FORM_STATUS_SUBMISSION_DLR) {
          //单一售后、RACE、SPACE店首笔提车款附件不必填
          if (types.DEALER_INFO_DEALER_TYPE_SINGLE_AFTER_SALE === value || types.DEALER_INFO_DEALER_TYPE_DIRECT_SHOP === value || types.DEALER_INFO_DEALER_TYPE_NO_DIRECT_SHOP === value) {
            //首笔提车款附件
            this.initForm.find(x => x.fieldName === 'firstPaymentEnclosureList').rules = [];
            //4S店和单一销售必填
          } else if (types.DEALER_INFO_DEALER_TYPE_4S === value || types.DEALER_INFO_DEALER_TYPE_SINGLE_SALE === value) {
            this.initForm.find(x => x.fieldName === 'firstPaymentEnclosureList').rules = [{ required: true, message: '必填', trigger: 'blur' }];
          } else {
            this.initForm.find(x => x.fieldName === 'firstPaymentEnclosureList').rules = [];
          }
        }
      },
      //表单数据
      initFormFun() {
        let bt = { required: true, message: '必填', trigger: ['change', 'blur'] };
        let btl = [];
        let sj = { pattern: /^[1][3,4,5,7,8][0-9]{9}$/, message: '请填写正确的手机号', trigger: ['change', 'blur'] };
        let email = { type: 'email', message: '请输入正确的邮箱', trigger: 'blur' };
        let jy = { disabled: true };
        let yc = { hidden: true };

      },

      createColumns(){
        return [
          {
            title: '操作',
            dataIndex: 'column-action',
            width: 50,
            fixed: 'left',
            render: (props, h) => {
              return (
                <div>
                <el-button size="mini" type="text" onClick={() => this.deleteRow(props.row,props.$index)}>
              删除
              </el-button>
              </div>
            );
            }
          },
          {
            title: '顺序',
            dataIndex: 'index',
            width: 80,
            sorter: true,
            render: props => {
              return <span>{props.$index + 1}</span>;
            }
          },
          {
            title: '岗位名称',
            dataIndex: 'stationName',
            filterType: 'input',
            sorter: true,
            filter: true,
            editable: true,
            editRequired: this.formType == 'add' || this.formType == 'edit',
            editType: 'select',
            editItems:this.createDictList('1006'),
            dictItems: this.createDictList('1006'),
            // editValidator: (value, row, callback) => {
            //   if (value === undefined || value === '' || value === null) {
            //     callback(new Error("必填字段"));
            //   }
            // }
          },
          {
            title: '姓名',
            dataIndex: 'employeeName',
            filterType: 'input',
            sorter: true,
            editable: true,
            editRequired: true,
            maxlength: 100,
            // editValidator: (value, row, callback) => {
            //   if (value === undefined || value === '' || value === null) {
            //     callback(new Error("必填字段"));
            //   }
            // }
          },
          {
            title: '联系方式',
            dataIndex: 'employeePhone',
            filterType: 'input',
            editRequired: true,
            sorter: true,
            editable: true,
            maxlength: 20,
            // editValidator: phoneAndSolidify,
            // editValidator: (value, row, callback) => {
            //   if (value === undefined || value === '' || value === null) {
            //     callback(new Error("必填字段"));
            //   }
            //   var pattern = /^1[3-9]\d{9}$/;
            //   var solidify = /^(0\d{2,3}\-)?([2-9]\d{6,7})+(\-\d{1,6})?$/;
            //   if (pattern.test(value) || solidify.test(value)) {
            //     callback();
            //   }else{
            //     callback(new Error('请输入正确的手机号或固话'));
            //   }
            // }
          },
          {
            title: '在职状态',
            dataIndex: 'employeeStatus',
            filterType: 'input',
            sorter: true,
            filter: true,
            editable: true,
            editRequired: this.formType == 'add' || this.formType == 'edit',
            editType: 'select',
            editItems:this.createDictList('1008'),
            dictItems: this.createDictList('1008'),
            // editValidator: (value, row, callback) => {
            //   if (value === undefined || value === '' || value === null) {
            //     callback(new Error("必填字段"));
            //   }
            // }
          },
          {
            title: '离职时间',
            dataIndex: 'dimissionDate',
            sorter: true,
            filter: true,
            filterType: 'date-range',
            showOverflowTooltip: true,
            editable: true,
            editType: 'date-picker',
            dateFormat: 'yyyy-MM-dd HH:mm:ss'
          },
          // {
          //   title: '是否通过认证',
          //   dataIndex: 'isCertificate',
          //   filterType: 'input',
          //   editRequired: true,
          //   sorter: true,
          //   editable: true,
          //   placeholder: '',
          //   rules: [{ required: true, message: '请输入', trigger: 'blur' }]
          // },
          // {
          //   title: '通过认证时间',
          //   dataIndex: 'certificateTime',
          //   filterType: 'input',
          //   editRequired: true,
          //   sorter: true,
          //   editable: true,
          //   placeholder: '',
          //   rules: [{ required: true, message: '请输入', trigger: 'blur' }]
          // },
          // {
          //   title: '在职状态',
          //   dataIndex: 'employeeStatus',
          //   filterType: 'input',
          //   editRequired: true,
          //   sorter: true,
          //   editable: true,
          //   placeholder: '',
          //   rules: [{ required: true, message: '请输入', trigger: 'blur' }]
          // },
          // {
          //   title: '离职时间',
          //   dataIndex: 'dimissionDate',
          //   filterType: 'input',
          //   editRequired: true,
          //   sorter: true,
          //   editable: true,
          //   placeholder: '',
          //   rules: [{ required: true, message: '请输入', trigger: 'blur' }]
          // },
        ];
      },

      deleteRow(row,index) {
        this.$confirm('确认删除', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.$refs.tableQuestion.EXECUTE_DELETE([row])
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            });
          });
      },

      syncFun(details){
        this.saveData.xxList = details
      },

      addFun(){
        var p = {
          positionName: '',
          name: '',
          linkTel: '',
          isCertificate: '',
          certificateTime: '',
          status: '',
          leavingDate: '',
        }
        this.$refs.tableQuestion.EXECUTE_INSERT(p)
      },

    }
  };
</script>
